﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using HA.ModFlow.Controls.MT3DMSWindows;

using HAUtil.DosRunUtil;

using HAUtilWpf.windows;

using HA.MT3DMS.DomainLayer;

namespace HA.ModFlow.Application

{


   public static class MT3DCommands
    {

       static MT3DCommands()
       {
           _BTNEdit = new RoutedCommand();
           _GCGEdit = new RoutedCommand();
           _ADVEdit = new RoutedCommand();
           _SSMEdit = new RoutedCommand();
           _DisEdit = new RoutedCommand();
           _CRPEdit = new RoutedCommand();



           _checkMT3DMS = new RoutedCommand();

           _runMT3DMS = new RoutedCommand();

           readModflowSetup = new RoutedCommand();






       }

       public static Window mainWindow;

       static RoutedCommand readModflowSetup;

       public static RoutedCommand ReadModflowSetup
       {
           get { return MT3DCommands.readModflowSetup; }
           
       }

       static RoutedCommand _calculateMass = new RoutedCommand();

       public static RoutedCommand CalculateMass
       {
           get { return MT3DCommands._calculateMass; }          
       }


       static RoutedCommand _checkMT3DMS;

       public static RoutedCommand CheckMT3DMS
       {
           get { return MT3DCommands._checkMT3DMS; }
           set { MT3DCommands._checkMT3DMS = value; }
       }


       static RoutedCommand _runMT3DMS;

       public static RoutedCommand RunMT3DMS
       {
           get { return MT3DCommands._runMT3DMS; }
           set { MT3DCommands._runMT3DMS = value; }
       }





       static RoutedCommand _BTNEdit;

       public static RoutedCommand BTNEdit
       {
           get { return MT3DCommands._BTNEdit; }
           set { MT3DCommands._BTNEdit = value; }
       }

       private static RoutedCommand _GCGEdit;

       public static RoutedCommand GCGEdit
       {
           get { return MT3DCommands._GCGEdit; }
           set { MT3DCommands._GCGEdit = value; }
       }

       private static RoutedCommand _ADVEdit;

       public static RoutedCommand ADVEdit
       {
           get { return MT3DCommands._ADVEdit; }
           set { MT3DCommands._ADVEdit = value; }
       }

       public  static RoutedCommand _SSMEdit;

       public static RoutedCommand SSMEdit
       {
           get { return MT3DCommands._SSMEdit; }
           set { MT3DCommands._SSMEdit = value; }
       }

       private static RoutedCommand _DisEdit;

       public static RoutedCommand DisEdit
       {
           get { return MT3DCommands._DisEdit; }
           set { MT3DCommands._DisEdit = value; }
       }

       private static RoutedCommand _CRPEdit;

       public static RoutedCommand CRPEdit
       {
           get { return MT3DCommands._CRPEdit; }
           set { MT3DCommands._CRPEdit = value; }
       }
       public static void BindCommandsToWindow(Window window)
       {
           CommandBinding cbBTN = new CommandBinding();
           cbBTN.Command = _BTNEdit;
           cbBTN.CanExecute+=new CanExecuteRoutedEventHandler(cbBTN_CanExecute);
           cbBTN.Executed+=new ExecutedRoutedEventHandler(cbBTN_Executed);
           window.CommandBindings.Add(cbBTN);

           CommandBinding cbGCG = new CommandBinding();
           cbGCG.Command = _GCGEdit;
           cbGCG.CanExecute += new CanExecuteRoutedEventHandler(cbGCG_CanExecute);
           cbGCG.Executed += new ExecutedRoutedEventHandler(cbGCG_Executed);
           window.CommandBindings.Add(cbGCG);

           CommandBinding cbADV = new CommandBinding();
           cbADV.Command = _ADVEdit;
           cbADV.CanExecute += new CanExecuteRoutedEventHandler(cbADV_CanExecute);
           cbADV.Executed += new ExecutedRoutedEventHandler(cbADV_Executed);
           window.CommandBindings.Add(cbADV);

           CommandBinding cbSSMEdit = new CommandBinding();
           cbSSMEdit.Command = _SSMEdit;
           cbSSMEdit.CanExecute += new CanExecuteRoutedEventHandler(cbSSMEdit_CanExecute);
           cbSSMEdit.Executed += new ExecutedRoutedEventHandler(cbSSMEdit_Executed);
           window.CommandBindings.Add(cbSSMEdit);

           CommandBinding cbDisEdit = new CommandBinding();
           cbDisEdit .Command =_DisEdit ;
           cbDisEdit .CanExecute +=new CanExecuteRoutedEventHandler(cbDisEdit_CanExecute);
           cbDisEdit.Executed += new ExecutedRoutedEventHandler(cbDisEdit_Executed);
           window.CommandBindings.Add(cbDisEdit);

           CommandBinding cbCRPEdit = new CommandBinding();
           cbCRPEdit.Command = _CRPEdit;
           cbCRPEdit.CanExecute += new CanExecuteRoutedEventHandler(cbCRPEdit_CanExecute);
           cbCRPEdit.Executed += new ExecutedRoutedEventHandler(cbCRPEdit_Executed);
           window.CommandBindings.Add(cbCRPEdit);





           CommandBinding cbCheckMT3DMS = new CommandBinding();
           cbCheckMT3DMS.Command = _checkMT3DMS;
           cbCheckMT3DMS.CanExecute += new CanExecuteRoutedEventHandler(cbCheckMT3DMS_CanExecute);
           cbCheckMT3DMS.Executed += new ExecutedRoutedEventHandler(cbCheckMT3DMS_Executed);
           window.CommandBindings.Add(cbCheckMT3DMS);

           CommandBinding cbRunMT3DMS = new CommandBinding();
           cbRunMT3DMS.Command = _runMT3DMS;
           cbRunMT3DMS.CanExecute += new CanExecuteRoutedEventHandler(cbRunMT3DMS_CanExecute);
           cbRunMT3DMS.Executed += new ExecutedRoutedEventHandler(cbRunMT3DMS_Executed);
           window.CommandBindings.Add(cbRunMT3DMS);


           CommandBinding cbReadModlfowSetup = new CommandBinding();
           cbReadModlfowSetup.Command = readModflowSetup;
           cbReadModlfowSetup.CanExecute += new CanExecuteRoutedEventHandler(cbReadModlfowSetup_CanExecute);
           cbReadModlfowSetup.Executed += new ExecutedRoutedEventHandler(cbReadModlfowSetup_Executed);
           window.CommandBindings.Add(cbReadModlfowSetup);

           CommandBinding cbCalculationMass = new CommandBinding();
           cbCalculationMass.Command = CalculateMass;
           cbCalculationMass.CanExecute += new CanExecuteRoutedEventHandler(cbCalculationMass_CanExecute);
           cbCalculationMass.Executed += new ExecutedRoutedEventHandler(cbCalculationMass_Executed);
           window.CommandBindings.Add(cbCalculationMass);
       }

       static void cbCalculationMass_CanExecute(object sender, CanExecuteRoutedEventArgs e)
       {
           if (!ProjectService.DefaultService.BNullProj)
           {
               if (ProjectService.DefaultService.Project.Mt3dmsData != null)
               {
                   e.CanExecute = true;

               }

           }
       }

       static void cbCalculationMass_Executed(object sender, ExecutedRoutedEventArgs e)
       {
           winCalculateMass win = new winCalculateMass(ProjectService.DefaultService.Project);
           win.Owner = mainWindow;
           win.ResizeMode = ResizeMode.NoResize;
           win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
           win.ShowDialog();

       }

       static void cbReadModlfowSetup_Executed(object sender, ExecutedRoutedEventArgs e)
       {

           MT3DMSData data = (MT3DMSData)e.Parameter;
           data.ResetModflowSetup(ProjectService.DefaultService.Project.MfData);
           

       }

       static void cbReadModlfowSetup_CanExecute(object sender, CanExecuteRoutedEventArgs e)
       {
           if (!ProjectService.DefaultService.BNullProj)
           {
               if (ProjectService.DefaultService.Project.Mt3dmsData != null)
               {
                   e.CanExecute = true;

               }

           }
       }

       static void cbRunMT3DMS_Executed(object sender, ExecutedRoutedEventArgs e)
       {

           //DosRun.ModFlowSolution solution = new DosRun.ModFlowSolution();

           //DosRun.SolutionRunObject runObj = new DosRun.SolutionRunObject(solution);

           //winRunSystem runWin = new winRunSystem();
           //runWin.RunSystem.AddRunInstance(runObj);



           //runWin.ShowDialog();

           ////如果确定执行了计算
           //if (runWin.IsHaveRun)
           //{
           //    string path = ProjectSerivce.DefaultService.GetProjPath(ProjPath.SOLUTION);

           //    ProjectSerivce.DefaultService.Project.MfData.ReadOutputResult(path);

           //    ProjectSerivce.DefaultService.Project.CurrentDataSet = Data.DataSetType.HEAD;
           //}


           DosRun.MT3DMSSolution solution = new DosRun.MT3DMSSolution();

           DosRun.MT3DMSRunObject runObj = new DosRun.MT3DMSRunObject(solution);


           winRunSystem runWin = new winRunSystem();
           runWin.RunSystem.AddRunInstance(runObj);

           runWin.Owner = mainWindow;
           runWin.ResizeMode = ResizeMode.NoResize;
           runWin.WindowStartupLocation = WindowStartupLocation.CenterScreen;

           runWin.ShowDialog();

           //如果确定执行了计算
           if (runWin.IsHaveRun)
           {

               DataOperationModule.DefaultModule.ReadMT3DResult();

               //ProjectSerivce.DefaultService.Project.CurrentDataSet = Data.DataSetType.HEAD;
           }



          
       }

       static void cbRunMT3DMS_CanExecute(object sender, CanExecuteRoutedEventArgs e)
       {
           if (!ProjectService.DefaultService.BNullProj)
           {
               if (ProjectService.DefaultService.Project.Mt3dmsData != null)
               {
                   e.CanExecute = true;

               }

           }

       }

       static void cbCheckMT3DMS_Executed(object sender, ExecutedRoutedEventArgs e)
       {
          
       }

       static void cbCheckMT3DMS_CanExecute(object sender, CanExecuteRoutedEventArgs e)
       {
           if (!ProjectService.DefaultService.BNullProj)
           {
               if (ProjectService.DefaultService.Project.Mt3dmsData!=null)
               {
                   e.CanExecute = true;
                   
               }
               
           }
       }

       static void cbCRPEdit_Executed(object sender, ExecutedRoutedEventArgs e)
       {
           winChemicalReation winchr = new winChemicalReation(ProjectService.DefaultService.Project.Mt3dmsData, ProjectService.DefaultService.Project);// new winChemicalReation(ProjectSerivce.DefaultService.Project);
           winchr.Owner = mainWindow;
           winchr.ResizeMode = ResizeMode.NoResize;
           winchr.WindowStartupLocation = WindowStartupLocation.CenterScreen;
           if ( winchr.ShowDialog().Value )
          {
              ProjectService.DefaultService.Project.Mt3dmsData.ChemicalReaction = winchr.PcChemical.ChemicalReaction;
          }
       }

       static void cbCRPEdit_CanExecute(object sender, CanExecuteRoutedEventArgs e)
       {
           if (ProjectService.DefaultService.BNullProj)
           {              
               e.CanExecute = false;
           }
           else
           {
               if (ProjectService.DefaultService.Project.Mt3dmsData != null)
               {
                   if (ProjectService.DefaultService.Project.Mt3dmsData.ChemicalReaction.Enabled)
                   {
                       e.CanExecute = true;
                   }
               }
           }
       }

       static void cbDisEdit_Executed(object sender, ExecutedRoutedEventArgs e)
       {         
          winDispersion windis = new winDispersion(ProjectService.DefaultService.Project  );
          windis.Owner = mainWindow;
          windis.ResizeMode = ResizeMode.NoResize;
          windis.WindowStartupLocation = WindowStartupLocation.CenterScreen;
           if (windis.ShowDialog().Value)
          {
              ProjectService.DefaultService.Project.Mt3dmsData.Dispersion = windis.Disper;
          }
       }

       static void cbDisEdit_CanExecute(object sender, CanExecuteRoutedEventArgs e)
       {
           if (ProjectService.DefaultService.BNullProj)
           {
               e.CanExecute = false;
           }
           else
           {
               if (ProjectService.DefaultService.Project.Mt3dmsData != null)
               {
                   if (ProjectService.DefaultService.Project.Mt3dmsData.Dispersion.Enabled)
                   {
                       e.CanExecute = true;
                   }
               }
           }
       }

       static void cbSSMEdit_Executed(object sender, ExecutedRoutedEventArgs e)
       {
           winSSMixingPack winssm = new winSSMixingPack( ProjectService.DefaultService.Project.Mt3dmsData.SourceSinkMixing,ProjectService.DefaultService.Project.Grid3dData);
           winssm.Owner = mainWindow;
           winssm.ResizeMode = ResizeMode.NoResize;
           winssm.WindowStartupLocation = WindowStartupLocation.CenterScreen;
           if (winssm.ShowDialog()==true)
           { 
               ProjectService.DefaultService.Project.Mt3dmsData.SourceSinkMixing.ResetMixingItems(winssm.SsMixing.Output, winssm.SsMixing.LstUsePrecious);
               ProjectService.DefaultService.Project.Mt3dmsData.SourceSinkMixing.GetRechAndEvt(winssm.SsMixing.RechargeSpConc, winssm.SsMixing.EvtSpConc);
           }
       }

       static void cbSSMEdit_CanExecute(object sender, CanExecuteRoutedEventArgs e)
       {
           if (ProjectService.DefaultService.BNullProj)
           {
               e.CanExecute = false;
           }
           else
           {
               if (ProjectService.DefaultService.Project.Mt3dmsData != null)
               {
                   if (ProjectService.DefaultService.Project.Mt3dmsData.SourceSinkMixing.Enabled)
                   {
                       e.CanExecute = true;
                   }
               }
           }
       }

       static void cbADV_Executed(object sender, ExecutedRoutedEventArgs e)
       {
           winAdvection winADV = new winAdvection(ProjectService.DefaultService.Project.Mt3dmsData);
           winADV.Owner = mainWindow;
           winADV.ResizeMode = ResizeMode.NoResize;
           winADV.WindowStartupLocation = WindowStartupLocation.CenterScreen;
           if (winADV.ShowDialog().Value)
           {
               ProjectService.DefaultService.Project.Mt3dmsData.Advection = winADV.Adv;
           }
       }

       static void cbADV_CanExecute(object sender, CanExecuteRoutedEventArgs e)
       {
           if (ProjectService.DefaultService.BNullProj)
           {
               e.CanExecute = false;
           }
           else
           {
               if (ProjectService.DefaultService.Project.Mt3dmsData != null)
               {
                   if (ProjectService.DefaultService.Project.Mt3dmsData.Advection.Enabled)
                   {
                       e.CanExecute = true;
                   }
               }
           }
       }

       static void cbGCG_Executed(object sender, ExecutedRoutedEventArgs e)
       {
           winGCGSolver winGCG = new winGCGSolver(ProjectService.DefaultService.Project.Mt3dmsData.Gcgolver);
           winGCG.Owner = mainWindow;
           winGCG.ResizeMode = ResizeMode.NoResize;
           winGCG.WindowStartupLocation = WindowStartupLocation.CenterScreen;
           if (winGCG.ShowDialog().Value)
           {
               ProjectService.DefaultService.Project.Mt3dmsData.Gcgolver = winGCG.Gcg;
           }
       }

       static void cbGCG_CanExecute(object sender, CanExecuteRoutedEventArgs e)
       {
           if (ProjectService.DefaultService.BNullProj)
           {
               e.CanExecute = false;
           }
           else
           {
               if (ProjectService.DefaultService.Project.Mt3dmsData != null)
               {
                   e.CanExecute = true;
               }
           }
         
       }

       static void cbBTN_CanExecute(object sender,CanExecuteRoutedEventArgs e)
       {
           if (ProjectService.DefaultService.BNullProj)
           {
               e.CanExecute = false;
           }
           else
           {
               if (ProjectService.DefaultService.Project.Mt3dmsData != null)
               {
                   e.CanExecute = true;
               }
           }
       }

       static void cbBTN_Executed(object sender, ExecutedRoutedEventArgs e)
       {
           winBasicTransport winBTN = new winBasicTransport(ProjectService.DefaultService.Project);
           winBTN.Owner = mainWindow;
           winBTN.ResizeMode = ResizeMode.NoResize;
           winBTN.WindowStartupLocation = WindowStartupLocation.CenterScreen;
           winBTN.ShowDialog();
       }
    }



}
